package com.wva.video.consumer.videoprocess;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.wva.common.client.CensorClient;
import com.wva.common.exception.BusinessException;
import com.wva.video.constant.ProcessConst;
import com.wva.video.domain.dto.VideoProcessDTO;
import com.wva.video.domain.po.Video;
import com.wva.video.enums.QueueEnum;
import com.wva.video.enums.VideoStatus;
import com.wva.video.service.VideoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * @Author: hong
 * @Date: 2025/2/17 15:33:39
 */
@Component
@RabbitListener(queues = "video.process.pre")
@Slf4j
public class PreProcessQueueConsumer {
    @Autowired
    CensorClient censorClient;
    @Autowired
    VideoService videoService;
    @Autowired
    RabbitTemplate rabbitTemplate;

    @RabbitHandler
    void handle(VideoProcessDTO videoProcessDTO) throws InterruptedException, JsonProcessingException {
        try {
            Long videoId = videoProcessDTO.getVideoId();
            log.info("视频{}: 预处理", videoId);

            //需要审核的信息
            Video video = videoService.getById(videoId);
            String title = video.getTitle();
            String description = video.getDescription();
            String[] tags = videoProcessDTO.getTags();

            //审核信息
            if (ProcessConst.INFO_CENSOR_ENABLED) {
                //调用审核微服务审核文本
                Boolean success = censorClient.censorText(
                        String.format("%s; %s; %s", title, description, Arrays.toString(tags))
                );

                //网络或阿里云服务问题，进行本地重试
                if (success == null) {
                    throw new BusinessException("审核微服务审核文本时发生异常");
                }

                //审核不通过
                if (!success) {
                    //更新视频状态
                    videoService.updateById(
                            Video.builder()
                                    .id(videoId)
                                    .status(VideoStatus.INFO_VIOLATION.getStatus())
                                    .reason("您的标题、描述或标签含有违反社区规范的内容")
                                    .build()
                    );
                    return;
                }
            }

            videoService.updateById(
                    Video.builder()
                            .id(videoId)
                            .status(VideoStatus.WAIT_MAIN_PROCESS.getStatus())
                            .build()
            );

            //审核通过，投送到处理队列
            rabbitTemplate.convertAndSend(
                    QueueEnum.VIDEO_PROCESS_MAIN.getExchange(),
                    QueueEnum.VIDEO_PROCESS_MAIN.getRoutingKey(),
                    videoProcessDTO
            );
        } catch (Exception e) {
            log.error("视频预处理出现异常", e);
            throw new RuntimeException(e);
        }
    }
}
